class UTProj_HomingRocket extends UTProj_Rocket;

var public Actor Shooter, Enemy;
var Vector Direction;
var float HomingStrength, obstacleAvoidanceLength, AvoidenceStrength, ignoringStart;
var bool debug, ignoreObstacles;

auto state FollowActor
{
	event Tick(float deltaTime)
	{
		FindDirection();
		if(!ignoreObstacles)
			Velocity = (Normal(Velocity)+SideAvoidence(Velocity)*AvoidenceStrength)*Speed;

		Velocity = (Normal(Velocity)+Direction*HomingStrength)*Speed;
		
		Velocity.Z += TossZ;
		SetRotation(rotator(Velocity));

		CheckDistance();
	}

	function FindDirection	()
	{
		Direction = Normal(Enemy.Location - Location);
	}

Begin:
	ignoreObstacles = false;
}



function CheckDistance()
{
	ignoreObstacles = VSize(Enemy.Location - Location) < ignoringStart;
}

function Vector SideAvoidence(Vector currentVector)
{
	local Rotator r;
	local Vector left,right,top,bottom;
	
	r = Rotator(currentVector);
	r.Yaw += 45*DegToUnrRot;
	right = Vector(r);
	
	r = Rotator(currentVector);
	r.Yaw += -45*DegToUnrRot;
	left = Vector(r);

	r = Rotator(currentVector);
	r.Pitch += -45*DegToUnrRot;
	bottom = Vector(r);

	r = Rotator(currentVector);
	r.Pitch += 45*DegToUnrRot;
	top = Vector(r);

	currentVector = ObstacleAvoidence(currentVector,false);
	currentVector +=ObstacleAvoidence(right,true);
	currentVector +=ObstacleAvoidence(left,true);
	currentVector +=ObstacleAvoidence(bottom,true);
	currentVector +=ObstacleAvoidence(top,true);
	
	return currentVector;
}

/**
 * Deze mehtode zorgt er voor dat bots niet tegen een muur aan lopen
 * @param currentVector - De huidige vector van het object, deze wordt aangepast volgens de obstacleavoidence
 * @param forceOnly - geeft alleen kracht terug en telt hem niet op bij de vector
 */
function Vector ObstacleAvoidence(Vector currentVector, bool forceOnly = false)
{
		local Vector HitLocation;
		local Vector HitNormal;
		local Vector ReturnVector;
		
		local float strength, distance;

		if(debug)
			DrawDebugLine(Location,Location+Normal(currentVector)*obstacleAvoidanceLength,250,0,0, false);
		Trace(HitLocation,HitNormal,Location+Normal(currentVector)*obstacleAvoidanceLength,Location,false);

		ReturnVector = MirrorVectorByNormal(currentVector,HitNormal);

		distance = VSize(HitLocation - Location);
		if(distance == 0 && !forceOnly)
			return currentVector;
		else if(distance == 0 )
			return vect(0,0,0);
		
		strength = (50000/distance) ;// * AvoidenceStrength;

		if(forceOnly)
			return Normal(ReturnVector)*strength;
		return currentVector + Normal(ReturnVector)*strength;
}

DefaultProperties
{
	HomingStrength = 0.3;
	debug = false;
	obstacleAvoidanceLength = 500;
	ignoringStart = 300;
	AvoidenceStrength = 0.3;
	Speed = 2000;

	ProjFlightTemplate=none
	ProjExplosionTemplate=ParticleSystem'explosie_particle.toon_explosie'

	Begin Object class=StaticMeshComponent name=projmesh
        StaticMesh =StaticMesh'Artalpha.Raketje'
		bUsePrecomputedShadows = true;	
		Rotation = (Yaw = 0, Pitch = -16383.96 , Roll = 0)
	End Object
    Components.Add(projmesh)
    ProjSMC = projmesh
}